<?php
class TeacherStartCourseDao {
	public $table = 'teacher_class';
	public function getByTeacherId($teacherId,$date,$time){
		return ClassManager::getSql()->onedata($this->table,'*',"where teacher_id='".intval($teacherId)."' and  `date`='".$date."' and `time`='".$time."' ");
	}
	
	public function searchList($where,$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}	

	public function searchWhere($where){
		$likeWhere = array();
		foreach ($where as  $k=>$v){
			if (isset($v)&&$v!==""){
				if (in_array($k,$likeWhere) and !is_array($v)){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}
				else{
					$where1[$k] =  $v;
				}
			}
		}
		return $where1;
	}	
	
	public function add($data){
		ClassManager::getSql()->insert($this->table, $data);
	}
	public function addAll($column_list,$data_list){
		ClassManager::getSql()->insertArray($this->table,$column_list, $data_list);
	}
	public function delete($teacherId,$date,$time){
		ClassManager::getSql()->delete($this->table, "where teacher_id='".intval($teacherId)."' and  `date`='".$date."' and `time`='".$time."' and status =1 ");
	}
	public function deleteAll($teacherId,$date){
		ClassManager::getSql()->delete($this->table, "where teacher_id='".intval($teacherId)."' and  `date`='".$date."' and status =1 ");
	}
	public function getListByTeacherId($teacherId,$date){
		return ClassManager::getSql()->datalist($this->table,'*',"where teacher_id='".intval($teacherId)."' and  `date`='".$date."'");
	}

	public function getOpenListByTeacherId($where){
		$teacherId= $where['teacher_id'];
		$date= $where['date'];
		$status= $where['status'];
		$confirm_status = $where['confirm_status'];
		return ClassManager::getSql()->datalist($this->table,'*',"where teacher_id='".intval($teacherId)."' and  `date`='".$date."' and  status ='".$status."' and confirm_status='".$confirm_status."'");
	}
	
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table,'id',intval($id));
	}
	
	public function update($data, $id){
		ClassManager::getSql()->updates($this->table,$data,"where id =".intval($id));
	}
	
	public function updateStatus($status, $id){
		$data['status'] = $status;
		ClassManager::getSql()->updates($this->table,$data,"where id =".intval($id));
	}

	public function updateConfirmStatus($confirm_status,$where){
		$data['confirm_status'] = $confirm_status;
		$teacher_id = $where['teacher_id'];
		$date = $where['date'];
		$time_list = $where['time_list'];
		ClassManager::getSql()->updates($this->table,$data,"where teacher_id =".intval($where['teacher_id'])." and date =".$where['date']." and time in ".$where['time_list']);
	}
	
	public function getTeacherTodayDateTime($teacherId,$date=''){
		if (empty($date)){
			$date = strtotime(date('Y-m-d'));
		}
		
		$status = TEACHER_CLASS_STATUS_NOTBOOKING;
		$confirm_status = TEACHER_STATUS_CONFIRMED;
		$start_time = $time  =(date('G')+1). date('i');
		return ClassManager::getSql()->datalist($this->table,'*',"where teacher_id='".intval($teacherId)."' and time>='".$start_time."' and  date='".$date."' and status='".$status."' and confirm_status = '".$confirm_status."'");
	}
	public function userGet($id,$date,$status=1){
		return ClassManager::getSql()->onedata($this->table,'*',"where   id='".intval($id)."'  and  `date`>='".$date."' and  status ='".$status."'  and confirm_status=2 ");
	}
	public function getListByGroupCount($where){
		$sql ="";
		$sql .= " select count(1) cnt from  ";
		$sql .= " (select c.id  ";
		$sql .= " from  (select a.id,a.`name`,a.network_stat,a.good_course,a.portrait,english_level,sex,a.introduce,if(b.date='".$where['date']."',1,0) as today_flg,b.date,b.time,education_experience,word_mouth from eo_teacher a  ";
		$sql .= " left join (select * from eo_teacher_class where confirm_status=2 and `status`=1 ";
		if(isset($where['search'])&&!empty($where['search'])){
			if(isset($where['date'])&&!empty($where['date'])){
				$sql.=" and date='".$where['date']."'";
			}
			if(isset($where['time'])&&intval($where['time'])>-1){
				$sql.=" and time='".$where['time']."'";
			}
		}
		$sql .= " ) b  on a.id=b.teacher_id where a.status in (".ON_WORK.",".PAUSE_WORK.")) c  where 1=1 "; 
		if(isset($where['good_course'])&&!empty($where['good_course'])){
			$sql.=" and c.good_course like '%|".$where['good_course']."|%'";
		} 
		
		if(isset($where['name'])&&!empty($where['name'])){
			$name = strtoupper($where['name']);
			$sql.=" and upper(c.name) like '%".$name."%'";
		} 
		
		if(isset($where['english_level'])&&!empty($where['english_level'])){
			$sql.=" and c.english_level >='".$where['english_level']."'";
		} 
		if(isset($where['sex'])&&!empty($where['sex'])){
			$sql.=" and c.sex ='".$where['sex']."'";
		} 

		if(isset($where['network_stat'])&&!empty($where['network_stat'])){
			$sql.=" and c.network_stat ='".$where['network_stat']."'";
		}
		
		if(isset($where['search'])&&!empty($where['search'])){
			if(isset($where['time'])&&intval($where['time'])>-1){
				$sql.=" and c.today_flg =1 ";
			}
		}
		$sql .= " group by c.id,c.name,c.portrait,c.introduce,c.education_experience,c.word_mouth) d ";
		$dataGroupCount =  ClassManager::getSql()->dataMutillist('teacher and teacher_class_count'.$where['date'],$sql);
		return $dataGroupCount[0][0];
	}
	public function getListByGroup($where,$limit){
		$sql = " select * from (select c.id,if(sum(c.today_flg)='0',0,c.pxh) pxh,c.name,c.portrait,c.list_portrait,c.introduce,sum(c.today_flg) today_flg,count(c.time),c.education_experience,c.word_mouth  ";
		$sql .= " from  (select a.id,a.pxh,a.network_stat,a.`name`,a.good_course,a.portrait,a.list_portrait,english_level,sex,a.introduce,if(b.date='".$where['date']."',1,0) as today_flg,b.date,b.time,education_experience,word_mouth from eo_teacher a  ";
		$sql .= " left join (select * from eo_teacher_class where confirm_status=2 and `status`=1 ";
		if(isset($where['search'])&&!empty($where['search'])){
			if(isset($where['date'])&&!empty($where['date'])){
				$sql.=" and date='".$where['date']."'";
			}
			if(isset($where['time'])&&intval($where['time'])>-1){
				$sql.=" and time='".$where['time']."'";
			}
		}
		$sql .= " ) b  on a.id=b.teacher_id where a.status in (".ON_WORK.",".PAUSE_WORK.")) c  where 1=1 "; 
		
		if(isset($where['good_course'])&&!empty($where['good_course'])){
			$sql.=" and c.good_course like '%|".$where['good_course']."|%'";
		} 
		
		if(isset($where['name'])&&!empty($where['name'])){
			$sql.=" and c.name like '%".$where['name']."%'";
		} 
		
		if(isset($where['english_level'])&&!empty($where['english_level'])){
			$sql.=" and c.english_level >='".$where['english_level']."'";
		} 
		
		if(isset($where['sex'])&&!empty($where['sex'])){
			$sql.=" and c.sex ='".$where['sex']."'";
		} 
		
		if(isset($where['network_stat'])&&!empty($where['network_stat'])){
			$sql.=" and c.network_stat ='".$where['network_stat']."'";
		} 
		
		if(isset($where['search'])&&!empty($where['search'])){
			if(isset($where['time'])&&intval($where['time'])>-1){
				$sql.=" and c.today_flg =1 ";
			}
		}

		$sql .= " group by c.id,c.name,c.portrait,c.introduce,c.education_experience,c.word_mouth) d ";
		$sql .= " order by 	d.pxh desc,d.today_flg desc ";
		return ClassManager::getSql()->dataMutillist('teacher and teacher_class'.$where['date'],$sql,$limit);
	}
}

?>